package com.dyl.vod.controller;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.dyl.common.core.RR;

import com.dyl.common.core.exception.BaseException;
import com.dyl.vod.service.VideoService;
import com.dyl.vod.utils.AliyunVodSDKUtils;
import com.dyl.vod.utils.VodCofigUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Api(description="视频管理")
@RestController
@RequestMapping("/uploadVideo")
@CrossOrigin
public class VideoAdminController {

    @Autowired
    VideoService videoService;

    @ApiOperation(value = "上传视频")
    @PostMapping("vod")
    public RR uploadVideo(@RequestBody MultipartFile file){

        try {
            //获取文件流
            InputStream inputStream = file.getInputStream();
            //获取文件名称
            String originalFilename = file.getOriginalFilename();
            //获取文件标题
            String title = originalFilename.substring(0,originalFilename.lastIndexOf("."));
            //keyid,密钥，标题，文件名称，文件流
            UploadStreamRequest request = new UploadStreamRequest(
                    VodCofigUtils.ACCESS_KEY_ID,
                    VodCofigUtils.ACCESS_KEY_SECRET,
                    title,
                    originalFilename,
                    inputStream
            );

            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            /* 如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因 */

            String videoId = response.getVideoId();
            return RR.ok().data("videoId",videoId).code(200).message("上传成功");

        } catch (IOException e) {
            e.printStackTrace();
            throw new BaseException("上传视频失败",20001,"阿里云VOS视频上传");
        }

    }

    @ApiOperation(value = "删除视频")
    @DeleteMapping("delVideo/{videoId}")
    public RR delVideo(@PathVariable("videoId") String videoId){

        try {
            //*初始化客户端对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    VodCofigUtils.ACCESS_KEY_ID,
                    VodCofigUtils.ACCESS_KEY_SECRET);
            //*创建请求对象（不同操作，类不同）
            DeleteVideoRequest request = new DeleteVideoRequest();
            //*创建响应对象（不同操作，类不同）
            //DeleteVideoResponse response = new DeleteVideoResponse();
            //*向请求中设置参数
            //支持传入多个视频ID，多个用逗号分隔
            request.setVideoIds(videoId);
            //*调用客户端对象方法发送请求，拿到响应
            client.getAcsResponse(request);
            return RR.ok();

        } catch (ClientException e) {
            e.printStackTrace();
            throw new BaseException("删除视频失败",20001,"阿里云VOS视频删除");
        }

    }

    @ApiOperation(value = "批量删除视频")
    @DeleteMapping("delVideoList")
    public RR delVideo(@RequestParam("videoIdList") List<String> videoIdList){
        try {
            //*初始化客户端对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(
                    VodCofigUtils.ACCESS_KEY_ID,
                    VodCofigUtils.ACCESS_KEY_SECRET);
            //*创建请求对象（不同操作，类不同）
            DeleteVideoRequest request = new DeleteVideoRequest();

            //*向请求中设置参数
            //支持传入多个视频ID，多个用逗号分隔
            //11,12,13
            String videoIds = org.apache.commons.lang.StringUtils.join(videoIdList.toArray(),",");
            request.setVideoIds(videoIds);
            //*调用客户端对象方法发送请求，拿到响应
            client.getAcsResponse(request);
            return RR.ok();

        } catch (ClientException e) {
            e.printStackTrace();
            throw new BaseException("批量删除视频失败",20001,"阿里云VOS视频批量删除");
        }

    }

    @ApiOperation(value = "获取视频点播地址")
    @GetMapping("getVideo/{videoId}")
    public RR getVideo(@PathVariable("videoId") String videoId){
        return RR.ok().data("url", videoService.getVideoPlayUrl(videoId));
    }





}
